package com.have.blog.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@SuppressWarnings("all")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>
                .ExpressionInterceptUrlRegistry registry
                = httpSecurity.authorizeRequests();

        // 允许跨域请求的OPTIONS
        registry.antMatchers(HttpMethod.OPTIONS)
                .permitAll()
                .antMatchers(
                "/swagger-ui.html",
                            "/swagger-ui/*",
                            "/swagger-resources/**",
                            "/v2/api-docs",
                            "/v3/api-docs",
                            "/webjars/**",
                            "/actuator/**",
                            "/druid/**")
                .permitAll()
                // 对于获取token的rest api要允许匿名访问
                .antMatchers("/index/**",
                        "/resource/**",
                        "/about/**",
                        "/labs/test/**",
                        "/search/**",
                        "/content/**",
                        "/sort/**",
                        "/classify/**",
                        "/tag/**",
                        "/subject/**",
                        "/login/**",
                        "/oauth/**",
                        "/freemarker/**",
                        "/sysDictData/**",
                        "/elasticSearchBlog/**",
                        "/wechat/**",
                        "/like/**",
                        "/web/comment/getUserReceiveCommentCount",
                        "/web/comment/getList")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf()
                .disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .exceptionHandling()
                .accessDeniedHandler(restfulAccessDeniedHandler())
                .authenticationEntryPoint(restfulAuthenticationEntryPoint())
                .and()
                .addFilterBefore(jwtAuthenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);

        // 禁用缓存
        httpSecurity.headers().cacheControl();
    }

    @Bean
    public JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter() {
        return new JwtAuthenticationTokenFilter();
    }

    @Bean
    public RestfulAccessDeniedHandler restfulAccessDeniedHandler() {
        return new RestfulAccessDeniedHandler();
    }

    @Bean
    public RestfulAuthenticationEntryPoint restfulAuthenticationEntryPoint() {
        return new RestfulAuthenticationEntryPoint();
    }

}
